#!/bin/bash
#author merci
#安装前先确保/etc/sysconfig/network内的HOSTNAME和/etc/hosts内的hostname一致
#kafka部署系统最低内存建议不要低于1G

#hosts主机名(network内的HOSTNAME一致)
hname=`hostname`
#定义kafka版本
kafka_ver='2.11-1.1.0'
#kafka部署目录
kafka_dir='/usr/local/kafka'

yum install -y java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64

wget http://mirror.bit.edu.cn/apache/kafka/1.1.0/kafka_${kafka_ver}.tgz
tar -zxvf kafka_${kafka_ver}.tgz
mv kafka_${kafka_ver} ${kafka_dir}
mkdir -p ${kafka_dir}/logs/kafka

#修改kafka配置
sed -i 's@^broker.id=.*@broker.id=1@g' ${kafka_dir}/config/server.properties
sed -i "s@^listeners=.*@listeners=PLAINTEXT://${hname}:9092@g" ${kafka_dir}/config/server.properties
sed -i "s@^log.dirs=.*@log.dirs=${kafka_dir}/logs/kafka@g" ${kafka_dir}/config/server.properties
sed -i "s@^zookeeper.connect=.*@zookeeper.connect=${hname}:2181@g" ${kafka_dir}/config/server.properties

#重写zookeeper配置
mkdir -p ${kafka_dir}/zookeeper
mkdir -p ${kafka_dir}/logs/zookeeper
cat > ${kafka_dir}/config/zookeeper.properties << EOF
dataDir=${kafka_dir}/zookeeper
dataLogDir=${kafka_dir}
clientPort=2181
maxClientCnxns=100
tickTime=2000
initLimit=10
EOF

echo -e "zookeeper started"
nohup /usr/local/kafka/bin/zookeeper-server-start.sh /usr/local/kafka/config/zookeeper.properties &
sleep 3
echo -e "kafka started"
nohup /usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties &